var util = require('../../utils/util.js');
var utilMd5 = require('../../utils/md5.js');
var app = getApp();
Page({
  data: {
    verson: app.globalData.verson,
    token: '',
    uid: 0,
    minHeight:0,
    coin:0.00,
    userinfo:{},
    actionSheetHidden: true,
    actionSheetItems: [
      { bindtap: 'Menu1', txt: '提现', link: 'tixian/tixian'},
      { bindtap: 'Menu2', txt: '充值', link: 'chongzhi/chongzhi'},
      { bindtap: 'Menu3', txt: '收支明细', link: 'mingxi/mingxi'}
    ],
    cata: false,
    isAuthUser: false,
    isShow: false
  },
  onLoad: function(data){
    var that = this;
    // var uid = data.uid;
    // this.setData({
    //   uid: uid
    // });
    app.LoginUserInfo(function(res){
      var authFail = res == -1;//用户拒绝授权
      if (authFail) {
        wx.hideLoading();
        app.reAuthorize(that.reGetUserInfo);
        return;
      }

      var cata = (parseInt(res.usercata) > 0) ? true : false;
      var authStr = res.auth;
      var isAuth = authStr.indexOf("15") >= 0 ? true : false;
      
      that.setData({
        uid: res.uid,
        cata: cata,
        token:res.token,
        isAuthUser: isAuth
      });

      util.api({
        data: {
          "uid": res.uid,
          "publickey": util.publiccode,
          "ac": "userinfo"
        },
        success: function (responseData) {
          that.setData({
            userinfo: responseData.data
          });
        }
      });

      util.api({
        method: "post",
        data: {
          "uid": res.uid,
          "publickey": util.publiccode,
          "ac": "coininfo",
          "privatekey": utilMd5.hexMD5(that.data.uid + 'coininfo' + that.data.token)
        },
        success: function (responseData) {
          that.setData({
            coin: ((responseData.data.coin / 100.0).toFixed(2) || 0)
          });
        }
      });

      util.api({
        data: {
          "publickey": util.publiccode,
          "ac": "wxcheck"
        },
        success: function (responseData) {
          that.setData({
            isShow: responseData.data.isShow
          });
        }
      });

    });
    
    wx.getSystemInfo({
      success: function(res) {
        that.setData({
          minHeight: res.windowHeight
        });
      }
    });
    
  },

  reGetUserInfo: function (e) {
    var that = this;
    console.log("reGetUserInfo: ", e);
    if (e == 1) {
      // wx.reLaunch({ //用这个会报错fail can not invoke relaunch in background
      wx.redirectTo({
        url: '/pages/mine/mine'
      })
    } else {
      wx.redirectTo({
        url: '/pages/mail-list/mail-list'
      })
    }
  },

  // tixian: function(){
  //   var coin = this.data.coin;
  //   if(!coin && coin < 50){
  //     app.dialog('50元以上可提现', 'loading');
  //   }else{
  //     //写提现
  //   }
  // },
  actionSheetTap: function () {
    this.setData({
      actionSheetHidden: !this.data.actionSheetHidden
    })
  },
  actionSheetbindchange: function () {
    this.setData({
      actionSheetHidden: !this.data.actionSheetHidden
    })
  },
  bindMenu1: function (e) {//提现
    this.setData({
      actionSheetHidden: !this.data.actionSheetHidden
    })
    this.goAtherPage(e);
  },
  bindMenu2: function (e) {//充值
    this.setData({
      actionSheetHidden: !this.data.actionSheetHidden
    })
    this.goAtherPage(e);
  },
  bindMenu3: function (e) {//收支明细
    this.setData({
      actionSheetHidden: !this.data.actionSheetHidden
    })
    this.goAtherPage(e);
  },
  goAtherPage: function(e){
      var url = e.currentTarget.dataset.link;
      if(url){
        wx.navigateTo({url: url})
      }
  },
  goAtherPageRedirect: function(e){
      var url = e.currentTarget.dataset.link;
      wx.redirectTo({url: url})
  },
  switch1Change:function(e){
    console.log(e);
    var that = this
    console.log(app.globalData);
    wx.openSetting({
      success: (res) => {
        wx.clearStorageSync();
        var inviteuid;
        if (typeof inviteuid == 'undefined' || !inviteuid || inviteuid <= 0) {
          inviteuid = 0;
        }
        //先判断缓存中有无数据
        var userInfo = wx.getStorageSync('userInfo');
        if (userInfo) {
          app.globalData.userInfo = userInfo;
          typeof cb == "function" && cb(this.globalData.userInfo)
        } else {
          //调用微信登录接口  
          wx.login({
            success: function (e) {
              wx.getUserInfo({
                success: function (res) {
                  var js_code = e.code;
                  //艺术家小程序登录地址
                  var loginUrl = 'https://api.artart.cn/index.php?app=w3g&mod=Public&act=loginWechatSmall';
                  wx.request({
                    url: loginUrl,
                    data: {
                      code: js_code,
                      encryptedData: res.encryptedData,
                      iv: res.iv,
                      inviteuid: inviteuid
                    },
                    method: 'GET', // OPTIONS, GET, HEAD, POST, PUT, DELETE, TRACE, CONNECT
                    // header: {}, // 设置请求的 header
                    success: function (response) {
                      if (response.data.status == 1) {
                        //登录成功，存储信息                    
                        app.globalData.userInfo = response.data.data;
                        wx.setStorageSync('userInfo', response.data.data);
                        console.log(app.globalData);
                        typeof cb == "function" && cb(app.globalData.userInfo)
                      } else {
                        wx.showToast({
                          title: response.data.message,
                          icon: 'error',
                          duration: 2000
                        })
                      }
                    },
                    fail: function (e) {
                      // fail
                      console.log(e);
                    }
                  })
                }
              })
            },
            fail: function (e) {
              console.log(e);
            }
          });
        }
      }
    })
  },

  //保存formId
  submitFormId: function (e) {
    console.log("formId: ", e.detail.formId);
    app.saveFormId(e.detail.formId);
  },

  goYTX_miniProgram: function () {
    // wx.showToast({
    //   title: '到艺天下小程序',
    // })
    wx.navigateToMiniProgram({
      appId: 'wx9a963256f88b06ed',
      path: 'pages/index/index?id=' + this.data.uid,
      extarData: {
        open: 'happy'
      },
      envVersion: 'release',
      success(res) {
        // wx.showToast({
        //   title: '打开成功',
        // })
      },
      fail(res) {
        // wx.showToast({
        //   title: '打开失败' + JSON.stringify(res),
        // })
      }
    })
  }
})